imageFormat = function(cellvalue, options, rowdata) {
    return '<img src="' + pathImage + cellvalue + '" />';
};

imageUnFormat = function(cellvalue, options) {
    return $('img', cellvalue).attr('src');
};

postError = function(requestData, errMessage, errNumber){
    if(errNumber == '') {
        openDialogError("No se puede determinar el error.");
    } else {
        openDialogError(errNumber +': ' + errMessage);
    }
};

function procesarConsultaSubProceso(source, parameters, fnc, dataType) {
	if(dataType != null || dataType != undefined) {
		_post = $.post(path + "jqgrid/" + source, parameters, function(request){}, dataType);
	}
	else {
		_post = $.post(path + "jqgrid/" + source, parameters);
	}
    _post.success(fnc);
}

function procesarSeleccion(idPanel, idx, _options, parameters) {
    procesarConsultaSubProceso('seleccionar', parameters, function(requestData){
        $("#" + idPanel).html(requestData);
        actualizarGrid(idx, _options);
    });
}

function procesarProcedimiento(idPanel, idx, _options, parameters, bindkeys, navGrid) {
    procesarConsultaSubProceso('registrar', parameters, function(requestData){
        $("#" + idPanel).html(requestData);
        actualizarGrid(idx, _options, bindkeys, navGrid);
    });
}

function procesarJSON(idPanel, idx, _options, bindkeys, navGrid) {
	html  = "<table id=" + idx + "></table>";
	html += "<div id=p" + idx + "></div>";
	html += "<input type='hidden' id='c" + idx + "' name='c" + idx + "' value='' />";                                  
                                    
	$("#" + idPanel).html(html);
    reloadJQGrid(idx, _options, bindkeys, navGrid);
}

function procesarProcedimientoJSON(idPanel, idx, _options, parameters, bindkeys, navGrid) {
	html  = "<table id=" + idx + "></table>";
	html += "<div id=p" + idx + "></div>";
	html += "<input type='hidden' id='c" + idx + "' name='c" + idx + "' value='' />";                                  
                                    
	$("#" + idPanel).html(html);
    procesarConsultaSubProceso('registrar', parameters, function(requestData){
    	$("#c" + idx).val(requestData.length);
    	_options = $.extend(_options, {
    		datatype: "local",
    		data: requestData
    	});
        reloadJQGrid(idx, _options, bindkeys, navGrid);
    });
}

function reloadJQGrid(id, _options, bindkeys, navGrid){
    options = $.extend({
        // scroll: 1,
    	// loadComplete: function (data){},
    	height: 250,
        data: [],
        datatype: "local",
        rowNum: 10,
        rownumbers: false,
        recordtext: "{0} - {1} de {2} elementos",
        emptyrecords: 'No hay resultados',
        pgtext: 'Pag: {0} de {1}',
        pager: "#p" + id,
        viewrecords: true,
        shrinkToFit: false,
        loadonce: true,
        scrollOffset: 1,		
		subGrid: false,
		footerrow: false,
		sortable: false
    }, _options);
    idx = "#" + id;
    $(idx).jqGrid(options);
    $(idx).jqGrid('setFrozenColumns');
    if(bindkeys != undefined || bindkeys != null) {
        $(idx).jqGrid('bindKeys', bindkeys);
    }
    if(navGrid != undefined || navGrid != null) {
        navGrid();
    };
}

function actualizarGrid(id, _options, bindkeys, navGrid){
	_url = path + "jqgrid/paginar?name=" + id;
    options = $.extend({
        url: _url,
        datatype: "json"
    }, _options);
	reloadJQGrid(id, options, bindkeys, navGrid);
}

function inicializarGrid(id, _options, bindkeys, navGrid){
	reloadJQGrid(id, _options, bindkeys, navGrid);
}

function contenidocomboContenedorjqGrid(selector,idsigma){
	var optionsjq = {};
   	optionsjq.value ="9999999999:Seleccionar";
   	optionsjq.defaultValue="9999999999";
   	
    _post = $.post(path + "util/combocontenedor", {"idsigma": idsigma}, function(request){
	    	$.each(request, function(i , columns){
	            var value = columns[0];
	            var label = columns[1];		
	            optionsjq.value +=';' + value + ':' + label;
	        });	
    	}, 'json');
    return optionsjq;
}

function contenidocomboContenedor(selectId, idsigma){
    _post = $.post(path + "util/combocontenedor", {"idsigma": idsigma}, function(request){
        $(selectId).html(contenidocombo(request));
    }, 'json');
}

function contenidocombo(data){
    var options = '<option value="9999999999">SELECCIONAR</option>';
    $.each(data, function(i , columns){
        var value = columns[0];
        var label = columns[1];		
        options +='<option value="' + value + '">' + label + '</option>';
    });	
    return options;
}

function deshabilitarComponente(id, sw){
    id = "#" + id;
	
    if (sw == true || sw == 1) {
        $(id).removeAttr('disabled');
    } else if (sw == false || sw != 1) {
        $(id).attr('disabled', 'disabled');
    }
}

function print(){
    $("#divPrincipal").jqprint();
}

function closeDialog(id) {   
    $('#'+id).dialog('close');
}

var openDialogData = function(url, data, width, height, title, id, fnc, buttons) {
	var _post;
    if(url != undefined) {
    	_post = $.post(path + url, data);
        _post.success(function(requestData){
            $(id).html(requestData);
			
            if(fnc != undefined && fnc != null && fnc != false) {
            	fnc();
            }
            
            if(width != undefined || width != null) $(id).dialog('option', 'width', width);
            if(title != undefined || title != null) $(id).dialog('option', 'title', title);
            if(height != undefined || height != null) $(id).dialog('option', 'height', height);
            if(buttons != undefined || buttons != null) $(id).dialog('option', 'buttons', buttons);
            $(id).dialog('open');
        });
    }
};

function openDialogDataFunction1(url, data, width, height, title, fnc, buttons) {
	openDialogData(url, data, width, height, title, '#jqDialog1', fnc, buttons);
}

function openDialogDataFunction2(url, data, width, height, title, fnc, buttons) {
	openDialogData(url, data, width, height, title, '#jqDialog2', fnc, buttons);
}

function openDialogDataFunction3(url, data, width, height, title, fnc, buttons) {
	openDialogData(url, data, width, height, title, '#jqDialog3', fnc, buttons);
}

function openDialogData1(url, data, width, height, title) {
	openDialogData(url, data, width, height, title, '#jqDialog1', null, null);
}

function openDialogData2(url, data, width, height, title) {
	openDialogData(url, data, width, height, title, '#jqDialog2', null, null);
}

function openDialog1(url, width, height, title) {
	openDialogData(url, {}, width, height, title, '#jqDialog1', null, null);
}

function openDialog2(url, width, height, title) {	
	openDialogData(url, {}, width, height, title, '#jqDialog2', null, null);
}

function openDialogConfirm1(contenido, width, buttons) {
    $('#jqConfirmacion1').html(contenido);
    if(width != undefined) $('#jqDialogConfirmacion1').dialog('option', 'width', width);
    if(buttons != undefined) $('#jqDialogConfirmacion1').dialog('option', 'buttons', buttons);
    $('#jqDialogConfirmacion1').dialog('open');
}

function openDialogConfirm2(contenido, width, buttons) {
    $('#jqConfirmacion2').html(contenido);
    if(width != undefined) $('#jqDialogConfirmacion2').dialog('option', 'width', width);
    if(buttons != undefined) $(id).dialog('option', 'buttons', buttons);
    $('#jqDialogConfirmacion2').dialog('open');
}

function openDialogError(contenido, width, height) {
    $('#jqError').html(contenido);
    if(width != undefined) $('#jqDialogError').dialog('option', 'width', width);
    $('#jqDialogError').dialog('open');
}

function openDialogWarning(contenido, width, height) {
    $('#jqWarning').html(contenido);
    if(width != undefined) $('#jqDialogWarning').dialog('option', 'width', width);
    $('#jqDialogWarning').dialog('open');
}

function openDialogInfo(contenido, width, height, buttons, close) {
    $('#jqInfo').html(contenido);
    if(width != undefined) $('#jqDialogInfo').dialog('option', 'width', width);
    if(buttons != undefined) $('#jqDialogInfo').dialog('option', 'buttons', buttons);
    if(close != undefined) $('#jqDialogInfo').dialog('option', 'close', close);
    $('#jqDialogInfo').dialog('open');
}

function mouseHover(idTable) {
    idTable = '#' +  idTable + " tbody";
	
    $(idTable).delegate('tr', 'hover', function() {
        $(this).toggleClass("ui-state-highlight").next().stop(true, true);
    });
}

function DoNavrow(theUrl) {
    window.open(theUrl, '_self');
}

function themeTextBox(selector) {
    if(selector == undefined || selector == null) {
        selector = ".ui-text";
    }
    $(selector).on("blur", function(){
        if($(this).hasClass("ui-text-highlight")) {
            $(this).removeClass("ui-text-highlight");
        }
    });
    $(selector).on("focus", function(){
        if(!$(this).hasClass("ui-text-highlight")) {
            $(this).addClass("ui-text-highlight");
        }
    });
}

function themeComboBox(selector) {
    if(selector == undefined || selector == null) {
    	selector = 'select:not(".notcombobox")';
    }
    
    _selector = $(selector);
    _selector.combobox();
    _selector.each(function(){
        var id = $(this).attr("id").replace('cbo', '#txt');        
        $(id).bind("focus", function(){
            $(this).toggleClass("ui-combobox-input-highlight");
        });
        $(id).bind("blur", function(){
            $(this).toggleClass("ui-combobox-input-highlight");
        }); 
    });
}

_FsFiltrarCombo = function(_datos,_datbus,_indexbus,_indexcod,_indexdes,_excep){

	_options='';
	_datbus=_datbus.toUpperCase();
	_cadena='';
	for(var i=0;i<_datos.length;i++){
		_cadena = _datos[i][_indexbus];
		if (_datbus.indexOf(_cadena) != -1 && _datos[i][_indexcod]!=_excep){
			descrip = _datos[i][_indexdes];
			ncodcon = _datos[i][_indexcod];
			_options +='<option value="'+ncodcon+'"> '+descrip+'</option>';
		}
	}
	return _options;
};

(function($) {
    $.fn.getCheckboxValues = function(){
        var values = [];
        var i = 0;
        this.each(function (){
            values[i++] = $(this).val();
        });
        return values;
    };
})(jQuery);


Number.prototype.decimal = function (num) { 
    pot = Math.pow(10,num); 
    return parseInt(this * pot) / pot; 
};

function NumberFormat(num, numDec, decSep, thousandSep){ 
    var arg; 
    var Dec; 
    Dec = Math.pow(10, numDec);  
    if (typeof(num) == 'undefined') return;  
    if (typeof(decSep) == 'undefined') decSep = ','; 
    if (typeof(thousandSep) == 'undefined') thousandSep = '.'; 
    if (thousandSep == '.') 
     arg=/./g; 
    else 
     if (thousandSep == ',') arg=/,/g; 
    if (typeof(arg) != 'undefined') num = num.toString().replace(arg,''); 
    num = num.toString().replace(/,/g, '.');  
    if (isNaN(num)) num = "0"; 
    sign = (num == (num = Math.abs(num))); 
    num = Math.floor(num * Dec + 0.50000000001); 
    cents = num % Dec; 
    num = Math.floor(num/Dec).toString();  
    if (cents < (Dec / 10)) cents = "0" + cents;  
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
     num = num.substring(0, num.length - (4 * i + 3)) + thousandSep + num.substring(num.length - (4 * i + 3)); 
    if (Dec == 1) 
     return (((sign)? '': '-') + num); 
    else 
     return (((sign)? '': '-') + num + decSep + cents); 
   } 

$(function(){
    $("#itemMenu1").menuBar({
        content: $("#itemMenu1").next().html(),
        showSpeed: 1,
        flyOut: true
    });
    
    $("#itemMenu2").menuBar({
        content: $("#itemMenu2").next().html(),
        showSpeed: 1,
        flyOut: true
    });
    
    $("#itemMenu3").menuBar({
        content: $("#itemMenu3").next().html(),
        showSpeed: 1,
        flyOut: true
    });
    
    themeTextBox();
    themeComboBox();
	
	/*
	var brw = new Browser();
	console.log('fullName: ' + brw.fullName);
	console.log('name: ' + brw.name);
	console.log('fullVersion: ' + brw.fullVersion);
	console.log('version: ' + brw.version);
	console.log('platform: ' + brw.platform);
	console.log('mobile: ' + brw.mobile);
	console.log('resolution: ' + brw.width + 'x' + brw.height);
	console.log('availWidth: ' + screen.availWidth);
	console.log('width: ' + screen.width);
	console.log('availHeight: ' + screen.availHeight);
	console.log('height: ' + screen.height);
	*/
	window.moveTo(screen.width - screen.availWidth, screen.height - screen.availHeight);
	window.resizeTo(screen.availWidth + screen.availWidth - screen.width, screen.availHeight + screen.availHeight - screen.height);
	
	// console.log("#west.height: " + $("#west").css("height"));
});
